package com.inspur.system.service.impl;

import java.util.Date;
import java.util.List;

import com.inspur.common.utils.SecurityUtils;
import com.inspur.common.utils.StringUtils;
import com.inspur.common.utils.uuid.UUID;
import com.inspur.system.domain.vo.SysUserSaveProjVo;
import com.inspur.system.dto.PmFlawDTO;
import com.inspur.system.dto.ProNumDTO;
import com.inspur.system.mapper.PmDynMapper;
import com.inspur.system.mapper.SysUserMapper;
import com.inspur.system.utils.PmDynUtils;
import com.inspur.system.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.inspur.system.mapper.PmFlawMapper;
import com.inspur.system.domain.PmFlaw;
import com.inspur.system.service.IPmFlawService;

/**
 * 缺陷Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-08
 */
@Service
public class PmFlawServiceImpl implements IPmFlawService 
{
    @Autowired
    private PmFlawMapper pmFlawMapper;

    @Autowired
    private PmDynMapper pmDynMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询缺陷
     * 
     * @param cFlawNo 缺陷主键
     * @return 缺陷
     */
    @Override
    public PmFlawDTO selectPmFlawByCFlawNo(String cFlawNo)
    {
        return pmFlawMapper.selectPmFlawByCFlawNo(cFlawNo);
    }

    /**
     * 查询缺陷列表
     * 
     * @param pmFlaw 缺陷
     * @return 缺陷
     */
    @Override
    public List<PmFlaw> selectPmFlawList(PmFlaw pmFlaw)
    {
        return pmFlawMapper.selectPmFlawList(pmFlaw);
    }

    /**
     * 查询缺陷列表
     *
     * @param pmFlaw 缺陷
     * @return 缺陷
     */
    @Override
    public List<PmFlawDTO> selectPmDTOFlawList(PmFlawDTO pmFlaw)
    {
        List<PmFlawDTO> pmFlawDTOList = pmFlawMapper.selectPmFlawDTOList(pmFlaw);
        for (PmFlawDTO pmFlawDTO : pmFlawDTOList) {
            Double cEstWork = pmFlawDTO.getcEstWork(); // 预估工时
            Double cRemWork = pmFlawDTO.getcRemWork(); // 剩余工时
            String cEstRemWork = cEstWork + "h/" + cRemWork + "h";
            pmFlawDTO.setcEstRemWork(cEstRemWork);
        }
        return pmFlawDTOList;
    }

    /**
     * 新增缺陷
     * 
     * @param pmFlaw 缺陷
     * @return 结果
     */
    @Override
    public int insertPmFlaw(PmFlaw pmFlaw)
    {
        String pmFlawNo = "12" + UUID.generateShortUuid();
        pmFlaw.setcFlawNo(pmFlawNo);
        Long userId = SecurityUtils.getUserId();
        pmFlaw.setcCrePer(userId+"");
        pmFlaw.setcUpdPer(userId+"");
        pmFlaw.settCreTm(new Date());
        pmFlaw.settUpdTm(new Date());
        pmDynMapper.insertPmDyn(PmDynUtils.addDynCom("创建了工作项","1",pmFlaw.getcFlawNo()));
        // 同步用户表
        List<SysUserSaveProjVo> users = UserUtils.updatePmNo(userId,pmFlaw.getcFlawPer(),pmFlaw.getcFlawNo(),pmFlaw.getcFlawHead());
        users.forEach(user -> userMapper.updateUserByProj(user));
        return pmFlawMapper.insertPmFlaw(pmFlaw);
    }

    /**
     * 修改缺陷
     * 
     * @param pmFlaw 缺陷
     * @return 结果
     */
    @Override
    public int updatePmFlaw(PmFlaw pmFlaw)
    {
        PmFlawDTO oldFlaw = pmFlawMapper.selectPmFlawByCFlawNo(pmFlaw.getcFlawNo());
        if (StringUtils.isNotEmpty(oldFlaw.getcFlawState())) {
            if (!oldFlaw.getcFlawState().equals(pmFlaw.getcFlawState())) {
                String updateState = PmDynUtils.updateState(oldFlaw.getcFlawState(),pmFlaw.getcFlawState());
                pmDynMapper.insertPmDyn(PmDynUtils.addDynCom(updateState,"3",pmFlaw.getcFlawNo()));
            }
        } else {
            if (StringUtils.isNotEmpty(pmFlaw.getcFlawState())) {
                String updateState = PmDynUtils.updateState("0",pmFlaw.getcFlawState());
                pmDynMapper.insertPmDyn(PmDynUtils.addDynCom(updateState,"3",pmFlaw.getcFlawNo()));
            }
        }
        pmDynMapper.insertPmDyn(PmDynUtils.addDynCom("修改了工作项","1",pmFlaw.getcFlawNo()));
        return pmFlawMapper.updatePmFlaw(pmFlaw);
    }

    /**
     * 批量删除缺陷
     * 
     * @param cFlawNos 需要删除的缺陷主键
     * @return 结果
     */
    @Override
    public int deletePmFlawByCFlawNos(String[] cFlawNos)
    {
        return pmFlawMapper.deletePmFlawByCFlawNos(cFlawNos);
    }

    /**
     * 删除缺陷信息
     * 
     * @param cFlawNo 缺陷主键
     * @return 结果
     */
    @Override
    public int deletePmFlawByCFlawNo(String cFlawNo)
    {
        return pmFlawMapper.deletePmFlawByCFlawNo(cFlawNo);
    }

    @Override
    public ProNumDTO selectPmFlawNum()
    {
        return pmFlawMapper.selectPmFlawNum();
    }
}
